<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <ContentWrap>
      <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入姓名"
            :clearable="true"
            @keyup.enter="getPage"
          />
        </el-form-item>
        <el-form-item label="入住编号" prop="checkInNo">
          <el-input
            v-model="queryParams.checkInNo"
            placeholder="请输入入住编号"
            :clearable="true"
            @keyup.enter="getPage"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-input
            v-model="queryParams.status"
            placeholder="请选择状态"
            :clearable="true"
            :disabled="isStatusDisabled"
          >
            <el-option
              v-for="dict in getIntDictOptions(DICT_TYPE.ELDER_CHECK_IN_STATUS)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleClickSearch">
            <Icon icon="ep:search" />
            搜索
          </el-button>
          <el-button @click="handleClickReset">
            <Icon icon="ep:refresh" />
            重置
          </el-button>
        </el-form-item>
      </el-form>
      <Table
        :columns="columns"
        :page-param="queryParams"
        :adaptive="true"
        :page-data="queryData"
        save-key="ElderCheckInSelect"
        @page-change="getPage"
        @row-dblclick="handleTableDblclick"
      />
    </ContentWrap>
  </Dialog>
</template>
<script setup name="ElderCheckInSelect" lang="ts">
import { formatDate } from '@/utils/formatTime'
import * as ElderCheckInApi from '@/api/elder/checkIn'
import { DICT_TYPE, getDictObj, getIntDictOptions } from '@/utils/dict'
const dialogTitle = ref('')
const isStatusDisabled = ref(false)
const dialogVisible = ref(false)
const queryFormRef = ref()
const queryParams = ref({
  name: undefined,
  checkInNo: undefined,
  status: undefined
})
const queryData = ref()
const columns = [
  {
    label: '入住编号',
    prop: 'checkInNo'
  },
  {
    label: '姓名',
    prop: 'elder.name'
  },
  {
    label: '身份证号',
    prop: 'elder.identityCard'
  },
  {
    label: '状态',
    prop: 'status',
    formatter: ({ status }) => getDictObj(DICT_TYPE.ELDER_CHECK_IN_STATUS, status).label
  },
  {
    label: '入住开始时间',
    prop: 'beginTime',
    formatter: ({ beginTime }) => formatDate(beginTime, 'YYYY-MM-DD')
  },
  {
    label: '入住预计结束时间',
    prop: 'endTime',
    formatter: ({ endTime }) => formatDate(endTime, 'YYYY-MM-DD')
  },
  {
    label: '床位名称',
    prop: 'bedInfo.bedName'
  },
  {
    label: '护理计划名称',
    prop: 'nursePlan.planName'
  },
  {
    label: '餐饮套餐名称',
    prop: 'restaurantCombo.comboName'
  },
  {
    label: '入住实交费用',
    prop: 'checkInFee'
  },
  {
    label: '减免费用',
    prop: 'reductionFee'
  },
  {
    label: '创建时间',
    prop: 'createTime'
  }
]
const emit = defineEmits(['selectValue'])

/** 打开弹窗 */
const open = (params) => {
  isStatusDisabled.value = false
  if ((params.status! = undefined)) {
    queryParams.value.status = params.status.value
    isStatusDisabled.value = true
  }
  dialogVisible.value = true
  getPage()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 搜索按钮操作 */
const handleClickSearch = () => {
  getPage()
}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 处理响应事件  */
const getPage = async () => {
  queryData.value = await ElderCheckInApi.query(queryParams.value)
}

/** 处理table行双击事件 */
const handleTableDblclick = (row) => {
  emit('selectValue', row)
  close()
}

/** 关闭弹窗 */
const close = async () => {
  dialogVisible.value = false
}
</script>
